package com.mzj.saas.jobs.application.service;

import com.mzj.saas.jobs.application.entity.ApprovalProcess;
import com.mzj.saas.jobs.application.repository.ApprovalProcessRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ApprovalProcessService {
    private static Logger log = LoggerFactory.getLogger(ApprovalProcessService.class);


    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private ApprovalProcessRepository approvalProcessRepository;

    public String work() throws Exception {

        //查询所有区域是区的
        String areaSQL=" SELECT id AS district_id FROM `sys_area` where LEVEL=3 ";
        //查询已存在的流程
        String approvalProcessSQL=" SELECT district_id FROM `saas_approval_process` ";

        List<Long> areaList = jdbcTemplate.queryForList(areaSQL,Long.class);
        List<Long> approvalProcessList=jdbcTemplate.queryForList(approvalProcessSQL,Long.class);

        areaList.removeAll(approvalProcessList);

        List<ApprovalProcess> list=new ArrayList<>();

        for(Long area : areaList){
            ApprovalProcess ap=new ApprovalProcess();
            ap.setDistrictId(area);
            ap.setApprovalProcess("5,4,3");
            ap.setProcessTagName("社区->街道->区/县");
            ap.setStatus(1);//状态：0停用 1启用 新增时默认启用
            ap.setCreateTime(new Date());
            ap.setLastUpdateTime(new Date());
            list.add(ap);
        }

        approvalProcessRepository.saveAll(list);

        return "SUCCESS";
    }



}
